<?php defined('SYSPATH') or die('No direct script access.');

class Model_User extends Model_Entity
{
	public function add_user($info)
	{
		return $this->add('user', $info);
	}
	public function get_user_info($user_id)
	{
		return DB::select()->from('user')->where('id','=',$user_id)->execute()->current();
	}
	public function edit_user_info($user_id, $info)
	{
		return DB::update('user')->set($info)->where('id','=',$user_id)->execute();
	}
	public function get_all_users($project_id = NULL, $option=NULL)
	{
		$query = DB::select()->from('user');
		return $query->execute()->as_array();
	}
	public function get_project_users($project_id, $option)
	{
		$query = DB::select('user.*', 'project_user.*',
			array(DB::select(DB::expr('count(*)'))
				->from('task_project_user')
				->where('assign_user_id','=', DB::expr('user.id')),'total_task'))->from('user');
		if ($option == 0)
			$query->where('id', 'NOT IN', DB::select('user_id')->from('project_user')->where('project_id','=',$project_id));
		else
			$query->join('project_user')->on('user.id','=','project_user.user_id')->where('project_user.project_id','=',$project_id);
		//echo $query->__toString();die();
		return $query->execute()->as_array();
	}
	public function get_user_info_by_email($email)
	{
		return DB::select()->from('user')->where('email','=',$email)->execute()->current();
	}
	public function exists_user($username, $password)
	{
		$user = DB::select()->from('user')
			->where('email', '=', $username)->and_where('password','=',$password)->execute()->as_array();
		return (count($user) > 0);
	}
	public function get_group()
	{	
	}
}